#!/bin/bash
source config.sh
cd $FIAS_DIR

for i in $( ls ADDROBJ*.DBF ); do
		echo item: $i
		TABLENAME=`echo $i | cut -d '.' -f 1`
		echo "deleteing old tables"
		mysql  -h $HOST -u$UNAME -p$PASSWORD $DB -e "DROP TABLE IF EXISTS $TABLENAME"
		echo dbf2mysql
		dbf2mysql -h $HOST -d $DB -t $TABLENAME -c -P $PASSWORD -U $UNAME $i
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'DELETE FROM  `'$TABLENAME'` WHERE `ENDDATE` < NOW( );'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER TABLE `'$TABLENAME'` DROP `AOID`, DROP `AUTOCODE`, DROP `CENTSTATUS`, DROP `CODE`, DROP `CURRSTATUS`, DROP `ENDDATE`, DROP `IFNSFL`, DROP `IFNSUL`, DROP `NEXTID`, DROP `OKATO`, DROP `OKTMO`, DROP `OPERSTATUS`, DROP `PLAINCODE`, DROP `PREVID`, DROP `STARTDATE`, DROP `TERRIFNSFL`, DROP `TERRIFNSUL`, DROP `UPDATEDATE`, DROP `CTARCODE`, DROP `EXTRCODE`, DROP `SEXTCODE`, DROP `LIVESTATUS`, DROP `NORMDOC`, DROP `STREETCODE`, DROP `AREACODE`, DROP `CITYCODE`, DROP `PLACECODE`, DROP `OFFNAME`;'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'DELETE FROM  `'$TABLENAME'` WHERE  `ACTSTATUS` =0;'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'UPDATE `'$TABLENAME'` SET POSTALCODE="0" WHERE `POSTALCODE` = ""'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER TABLE `'$TABLENAME'` DROP `ACTSTATUS`;'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER TABLE `'$TABLENAME'` CHANGE `POSTALCODE` `POSTALCODE` INT(6) NOT NULL, CHANGE `REGIONCODE` `REGIONCODE` INT(2) NOT NULL;'

		echo "mysqldump into out.sql"
		mysqldump -h $HOST -u$UNAME -p$PASSWORD --default-character-set=latin1 $DB $TABLENAME >out.sql
		echo iconv
		iconv -f cp866 -t utf-8 out.sql>outtmp.sql
		echo "sed s/latin1/utf8/"
		more outtmp.sql|sed 's/latin1/utf8/'>out.sql
		echo "deleteing old tables"
		mysql  -h $REMOTEHOST -u$UNAME -p$PASSWORD --show-warning $DB -e "DROP TABLE IF EXISTS $TABLENAME"
		
		echo "mysql import"
		mysql  -h $REMOTEHOST -u$UNAME -p$PASSWORD --show-warning $DB  < out.sql
		mysql  -h $REMOTEHOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER IGNORE TABLE `'$TABLENAME'` ADD UNIQUE(`AOGUID`);'
		rm out.sql outtmp.sql
done
for i in $( ls HOUSE[0-9]*.DBF ); do
		echo item: $i
		TABLENAME=`echo $i | cut -d '.' -f 1`
		echo "deleteing old tables"
		mysql  -h $HOST -u$UNAME -p$PASSWORD  $DB -e "DROP TABLE IF EXISTS $TABLENAME"
		echo dbf2mysql
		dbf2mysql -h $HOST -d $DB -t $TABLENAME -c -P $PASSWORD -U $UNAME $i
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'DELETE FROM `'$TABLENAME'` WHERE `ENDDATE` < NOW( )'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER TABLE `'$TABLENAME'` DROP `BUILDNUM`, DROP `ENDDATE`, DROP `ESTSTATUS`, DROP `HOUSEID`, DROP `STATSTATUS`, DROP `IFNSFL`, DROP `IFNSUL`, DROP `OKATO`, DROP `OKTMO`, DROP `STARTDATE`, DROP `STRUCNUM`, DROP `STRSTATUS`, DROP `TERRIFNSFL`, DROP `TERRIFNSUL`, DROP `UPDATEDATE`, DROP `NORMDOC`, DROP `COUNTER`;'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'UPDATE `'$TABLENAME'` SET POSTALCODE="0" WHERE `POSTALCODE` = ""'
		mysql  -h $HOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER TABLE `'$TABLENAME'` CHANGE `POSTALCODE` `POSTALCODE` INT(6) NOT NULL;'

		echo "mysqldump into out.sql"
		mysqldump -h $HOST -u$UNAME -p$PASSWORD --default-character-set=latin1 $DB $TABLENAME >out.sql
		echo iconv
		iconv -f cp866 -t utf-8 out.sql>outtmp.sql
		echo "sed s/latin1/utf8/"
		more outtmp.sql|sed 's/latin1/utf8/'>out.sql
		echo "deleteing old tables"
		mysql  -h $REMOTEHOST -u$UNAME -p$PASSWORD --show-warning $DB -e "DROP TABLE IF EXISTS $TABLENAME"
		
		echo "mysql import"
		mysql  -h $REMOTEHOST -u$UNAME -p$PASSWORD --show-warning $DB  < out.sql
		mysql  -h $REMOTEHOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER IGNORE TABLE `'$TABLENAME'` ADD UNIQUE(`HOUSEGUID`);'
		mysql  -h $REMOTEHOST -u$UNAME -p$PASSWORD --show-warning $DB -e 'ALTER TABLE `'$TABLENAME'` ADD INDEX(`AOGUID`);'
		rm out.sql outtmp.sql
done

